﻿#region

using System;
using System.Collections.Generic;
using Autofac;
using Autofac.Integration.Web;
using BTL.Technology.Framework.Extensions;

#endregion

namespace BTL.Technology.Framework
{
    /// <summary>
    /// ObjectFactory class should be a singleton object.
    /// We can call it anywhere in the system, but we should not call it in any Presentation layer, 
    /// because in this layer we have another context for asp.net mvc
    /// </summary>
    public class ObjectFactory
    {
        private static ContainerProvider _containerProvider;

        // prevent initialized twice times
        private ObjectFactory()
        {
        }

        public static void SetLifetimeScope(IContainer container)
        {
            _containerProvider = new ContainerProvider(container);
        }

        public static T GetType<T>()
        {
            T instance;

            _containerProvider.ApplicationContainer.TryResolve(out instance);

            return instance;
        }

        public static object GetType(Type type)
        {
            return _containerProvider.ApplicationContainer.TryResolve(out type);
        }

        public static IEnumerable<T> GetTypes<T>()
        {
            return _containerProvider.ApplicationContainer.ResolveAll<T>();
        }
    }
}